package com.hktf.moedokilock.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hktf.corelib.eventBus.LoginEvent;
import com.hktf.corelib.jsonbean.response.BooleanResult;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.Config;
import com.hktf.corelib.utils.LogMds;
import com.hktf.corelib.utils.SharePreUtils;

import com.hktf.moedokilock.R;
import com.hktf.moedokilock.activity.AboutActivity;
import com.hktf.moedokilock.activity.UserInfoActivity;
import com.hktf.moedokilock.activity.VipPayActivity;
import com.hktf.moedokilock.activity.YaoQingActivity;
import com.hktf.moedokilock.adapter.MineAdapter2;
import com.hktf.moedokilock.bean.MineItem;
import com.hktf.uilib.activity.ProtocolWebViewActivity;
import com.hktf.uilib.app.BaseActivity;
import com.hktf.uilib.callback.BaseCallBackNew;
import com.hktf.uilib.engine.DriverEngine;
import com.hktf.uilib.utils.DlgUtils;
import com.hktf.uilib.utils.ToastUtils;
import com.lzy.okgo.model.Response;

import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


/**
 * date: 2023/3/15 17:19.
 *
 * @auther: cdz
 * @function:
 */
public class MineFragment extends BaseFragment implements View.OnClickListener, BaseQuickAdapter.OnItemClickListener {
    private View view;
    private TextView tv_name, tvId;
    private RecyclerView recyclerView;
    private MineAdapter2 myItemAdapter;
    private FrameLayout fl;
    private LinearLayout ll;
    private TextView tvName;
    private LinearLayout llId;
    private TextView tvCopy;
    private ImageView ivVip;
    private TextView tvVip1;
    private TextView tvVip2;
    private TextView tvVip;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine_layout, container, false);
        BaseActivity activity = (BaseActivity) getActivity();
        activity.setRootViewPaddingTop(view.findViewById(R.id.fl));
        tv_name = view.findViewById(R.id.tv_name);
        tvId = view.findViewById(R.id.tv_id);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        view.findViewById(R.id.ll).setOnClickListener(v -> {
            startActivity(new Intent(getContext(), UserInfoActivity.class));
        });
        view.findViewById(R.id.tv_vip).setOnClickListener(v -> {
            startActivity(new Intent(getContext(), VipPayActivity.class));
        });
        view.findViewById(R.id.tv_copy).setOnClickListener(v -> {
            CommUtils.copy(Config.USERINFO.getUserId(), getContext());
        });

        initView();
        initData();
        return view;
    }

    String[] items;
    private List<MineItem> list;

    private void initData() {
        items = getResources().getStringArray(R.array.my_item);
        String updatetime = SharePreUtils.getUseTime(getContext(), SharePreUtils.UPDATEDATE, "");
        list = new ArrayList<MineItem>() {
            {
                add(new MineItem(items[0], com.hktf.uilib.R.drawable.white_m_corner_shape, 0, false, 0, "", ""));
                add(new MineItem(items[1], com.hktf.uilib.R.drawable.white_top_mcorner_bg, 0, false, 0, "", ""));
                add(new MineItem(items[2], com.hktf.uilib.R.drawable.white_bg, 0, false, 0, "", ""));
                add(new MineItem(items[3], com.hktf.uilib.R.drawable.white_bottom_mcorner_bg, 0, false, 0, "", ""));
            }
        };
        list.get(1).setShowMargin(true);

        list.get(1).setShowLine(true);
        list.get(2).setShowLine(true);


        myItemAdapter = new MineAdapter2(list);
        recyclerView.setAdapter(myItemAdapter);
        myItemAdapter.setOnItemClickListener(this);


        fillUserInfo();

    }

    private void fillUserInfo() {
        if (Config.USERINFO == null) {
            return;
        }
        tv_name.setText(Config.USERINFO.getNickName());
        tvId.setText(Config.USERINFO.getUserId());

        String vipExpireTime1 = Config.USERINFO.getVipExpireTime1();
        if (StringUtils.isNotEmpty(vipExpireTime1)) {
            //会员
            ivVip.setVisibility(View.GONE);
            tvVip1.setText(getString(com.hktf.uilib.R.string.vip_expire_time));
            tvVip2.setText(vipExpireTime1);
            tvVip.setText(getString(com.hktf.uilib.R.string.vip_open1));
        }else {
            tvVip.setText(getString(com.hktf.uilib.R.string.vip_open));
            ivVip.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_confirm) {
            // TODO: 2022/12/4 退出登录

        } else if (id == R.id.tv_name) {
        }
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        LogMds.i("cdz", "点击了：" + position);
        switch (position) {
            case 0:
                //邀请与奖励
                startActivity(new Intent(getContext(), YaoQingActivity.class));

                break;
            case 1:
                Intent intent = new Intent(getContext(), ProtocolWebViewActivity.class);
                intent.putExtra("flag", 1);
                startActivity(intent);
                break;
            case 2:
                Intent intent1 = new Intent(getContext(), ProtocolWebViewActivity.class);
                intent1.putExtra("flag", 0);
                startActivity(intent1);

                break;
            case 3:
                //关于我们
                startActivity(new Intent(getContext(), AboutActivity.class));

                break;
            case 4:

                break;
            case 5:
                //举报
                break;
            case 6:
                //上传日志

                break;


        }
    }
    private void sendFeedback(String content){
        showLoadingSend();
        DriverEngine.sendFeedBack(content, new BaseCallBackNew<BooleanResult>(BooleanResult.class) {

            @Override
            public void onSuccess(Response<BooleanResult> response) {
                showInfoDlg("已经收到您的举报信息，我们会尽快处理，请您耐心等候！",null);
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            EventBus.getDefault().unregister(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginEvent event) {
        //        LogMds.w("cdz", "收到消息了............isOnPause=" + isOnPause);
        if (event != null) {
            int event_id = event.getEvent_id();
            switch (event_id) {
                case LoginEvent.LOGIN_SUCCESS:
                    fillUserInfo();
                    break;
                case LoginEvent.CHANGE_USERINFO:
                    fillUserInfo();
                    break;
            }
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        LogMds.i("cdz", "mineOnResume");
        refreshData();
    }

    private void refreshData() {
    }


    private void initView() {
        fl = (FrameLayout) findViewById(R.id.fl);
        ll = (LinearLayout) findViewById(R.id.ll);
        tvName = (TextView) findViewById(R.id.tv_name);
        llId = (LinearLayout) findViewById(R.id.ll_id);
        tvCopy = (TextView) findViewById(R.id.tv_copy);
        ivVip = (ImageView) findViewById(R.id.iv_vip);
        tvVip1 = (TextView) findViewById(R.id.tv_vip1);
        tvVip2 = (TextView) findViewById(R.id.tv_vip2);
        tvVip = (TextView) findViewById(R.id.tv_vip);
    }
    public final <T extends View> T findViewById(@IdRes int id) {
        return view.findViewById(id);
    }
}
